package com.zixun.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.itheima.loopviewpager.LoopViewPager;
import com.zixun.DataUtils.DataUtils;
import com.zixun.Models.Message;
import com.zixun.Models.MessageItem;
import com.zixun.R;
import com.zixun.Views.PagerSlidingTabStrip;
import com.zixun.activity.HomeDetailActivity;
import com.zixun.adapter.FragmentAdapter;
import com.zixun.adapter.MessageAdapter;
import com.zixun.db.DatabaseUtil;

import java.util.ArrayList;
import java.util.List;


public class HomeFragment extends Fragment {
    //private RecyclerView recyclerView;

    //private List<MessageItem> datas;
    //private MessageAdapter adapter;

    private PagerSlidingTabStrip pts_tabs;
    private ViewPager vp_type;
    private LoopViewPager loopViewPager;
    private TextView center_text;
    private DatabaseUtil mUtil;
    private List<Message> list = null;
    /*  public HomeFragment(Context context) {
          //initData(context);
      }*/
    private List<Fragment> fragmentList;

    private String[] mTitles = new String[]{
            "成长发展", "婚恋问题", "人际关系", "心理障碍"
    };

    @Nullable
    @Override
    public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment, container, false);

        center_text = (TextView) view.findViewById(R.id.center_text);
        pts_tabs = (PagerSlidingTabStrip) view.findViewById(R.id.pts_tabs);
        vp_type = (ViewPager) view.findViewById(R.id.vp_material);
        center_text.setText("首页");

        loopViewPager = (LoopViewPager) view.findViewById(R.id.lvp_pager);
        loopViewPager.setImgData(DataUtils.imgListString());
        loopViewPager.setTitleData(DataUtils.titleListString());
        loopViewPager.start();
        initView();
        return view;
    }

    private void initView() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new GrowUpFragment(getContext()));
        fragmentList.add(new LoveFragment(getContext()));
        fragmentList.add(new RelationshipFragment(getContext()));
        fragmentList.add(new MentalDisorderFragment(getContext()));

        FragmentAdapter adapter =
                new FragmentAdapter(getChildFragmentManager(), fragmentList, mTitles);

        try {
            vp_type.setAdapter(adapter);

            vp_type.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                }

                @Override
                public void onPageSelected(int position) {

                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

        pts_tabs.setViewPager(vp_type);
    }

    /*private void initData(Context context) {
        //获取数据库
        mUtil = new DatabaseUtil(context);
        list = mUtil.queryAll();
        datas = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            MessageItem item = new MessageItem();
            switch (i){
                case 0:
                    item.setContent(list.get(i).getTitle());
                    item.setImageId(R.mipmap.dengji);
                    datas.add(item);
                    break;
                case 1:
                    item.setContent(list.get(i).getTitle());
                    item.setImageId(R.mipmap.yibing);
                    datas.add(item);
                    break;
                case 2:
                    item.setContent(list.get(i).getTitle());
                    item.setImageId(R.mipmap.qiangpo);
                    datas.add(item);
                    break;
                case 3:
                    item.setContent(list.get(i).getTitle());
                    item.setImageId(R.mipmap.kongju);
                    datas.add(item);
                    break;
                case 4:
                    item.setContent(list.get(i).getTitle());
                    item.setImageId(R.mipmap.jianyi);
                    datas.add(item);
                    break;
            }
        }
    }*/
}
